import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';

/// 注册登录用的输入框
loginRegistTextField(
    {String? hintText,
    Color hintColor = Colors.black12,
    double fontSize = 18,
    double height = 100,
    Color textColor = Colors.black45,
    TextEditingController? controller,
    bool isPwd = false,
    Widget? rightIcon}) {
  return TextField(
    obscureText: isPwd,
    controller: controller,
    style: TextStyle(
      letterSpacing: 2,
      color: textColor,
      fontSize: (fontSize),
    ),
    decoration: new InputDecoration(
      contentPadding: EdgeInsets.fromLTRB(25, 14, 0, 14),
      filled: true,
      fillColor: Colors.white,
      hintText: hintText,
      hintStyle: TextStyle(
        color: hintColor,
        fontSize: fontSize,
      ),
      border: OutlineInputBorder(
        borderRadius: BorderRadius.circular(30),
        borderSide: BorderSide.none,
      ),
      suffixIcon: Padding(
        padding: EdgeInsets.fromLTRB(0, 0, 10, 0),
        child: rightIcon,
      ),
    ),
  );
}

/// 侧滑的Item
slidableItem({
  required Widget child,
  EdgeInsets margin = EdgeInsets.zero,
  List<IconSlideAction>? actions,
}) {
  return Container(
    margin: margin,
    child: Slidable(
      actionPane: SlidableScrollActionPane(), // 滑出选项的面板 动画
      actionExtentRatio: 0.25,
      child: child,
      secondaryActions: actions,
    ),
  );
}

/// 侧滑的"删除"类型action
slidableDeleteAction({
  VoidCallback? onTap,
}) {
  return IconSlideAction(
    color: Colors.red,
    iconWidget: _slidableActionItem(
      '删除',
      Icons.delete,
    ),
    closeOnTap: true,
    onTap: onTap,
  );
}

/// 侧滑的"更多"类型action
slidableNormalAction({
  VoidCallback? onTap,
  String label = '更多',
  IconData? icon,
  Color color = Colors.black45,
}) {
  return IconSlideAction(
    color: Colors.red,
    iconWidget: _slidableActionItem(
      label,
      icon ?? Icons.more_horiz,
    ),
    closeOnTap: true,
    onTap: onTap,
  );
}

/// 侧滑按钮样式
_slidableActionItem(String caption, IconData icon) {
  return Column(
    mainAxisSize: MainAxisSize.min,
    children: [
      Flexible(
        child: Icon(
          icon,
          color: Colors.white,
          size: (28),
        ),
      ),
      Flexible(
        child: Text(
          caption,
          style: TextStyle(
            color: Colors.white,
            fontSize: (16),
          ),
        ),
      ),
    ],
  );
}

/// 未读数
unreadCoundWidget(int unreadCount) {
  if (unreadCount == 0) return Container();
  String count;
  if (unreadCount > 99) {
    count = '99+';
  } else {
    count = unreadCount.toString();
  }
  return Container(
    padding: EdgeInsets.only(left: (3), right: (3)),
    decoration: BoxDecoration(
      color: Colors.red,
      borderRadius: BorderRadius.all(
        Radius.circular((15)),
      ),
    ),
    constraints: BoxConstraints(
      minWidth: (18),
      maxWidth: (30),
    ),
    child: Text(
      count,
      maxLines: 1,
      textAlign: TextAlign.center,
      style: TextStyle(
        fontSize: (12),
        color: Colors.white,
      ),
    ),
  );
}

/// 根据毫秒返回时间Str
String timeStrByMs(int ms, {bool showTime = false}) {
  String ret = '';
  // 是否当天
  // HH:mm
  if (DateUtil.isToday(ms)) {
    ret = DateUtil.formatDateMs(ms, format: 'HH:mm');
  }
  // // 是否本周
  // // 周一、周二、周三...
  // else if (DateUtil.isWeek(ms)) {
  //   ret = DateUtil.getWeekdayByMs(ms);
  // }

  // 是否本年
  // MM/dd
  else if (DateUtil.yearIsEqualByMs(ms, DateUtil.getNowDateMs())) {
    if (showTime) {
      ret = DateUtil.formatDateMs(ms, format: 'MM月dd日 HH:mm');
    } else {
      ret = DateUtil.formatDateMs(ms, format: 'MM月dd日');
    }
  }
  // yyyy/MM/dd
  else {
    if (showTime) {
      ret = DateUtil.formatDateMs(ms, format: 'yyyy年MM月dd日 HH:mm');
    } else {
      ret = DateUtil.formatDateMs(ms, format: 'yyyy年MM月dd日');
    }
  }

  return ret;
}

/// 时间转换string
